package com.zoe.onelink.logging.trace.sql;

import lombok.NonNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-03-08
 */
public class SqlExecuteContextHolder {

    public static final String SQL = "SQL";

    public static final String PARAM = "PARAM";

    public static final String CONSUME = "CONSUME";

    public static final String ROWS = "ROWS";

    private static final ThreadLocal<Map<String, String>> HOLDER = ThreadLocal.withInitial(() -> new HashMap<>(2));

    public static Optional<String> get(@NonNull String key) {
        Map<String, String> map = HOLDER.get();
        if (map == null) {
            return Optional.empty();
        }
        return Optional.ofNullable(map.get(key));
    }

    public static void set(@NonNull String key, String value) {
        Map<String, String> map = HOLDER.get();
        if (map == null) {
            HOLDER.set(new HashMap<>(2));
        }
        HOLDER.get().put(key, value);
    }

    public static void remove() {
        HOLDER.remove();
    }


}
